﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CQIE.TES.IService;
using CQIE.TES.Model;
using Newtonsoft.Json.Linq;

namespace CQIE.TES.WEB.ADMIN.Controllers.API
{
    public class CourseAPIController : ApiController
    {
        public ICourseService CourseService { get; set; }

        public ITeacherService TeacherService { get; set; }

        [HttpGet]
        [Route("api/getcourseinfo")]
        public object GetCourse(int page,int limit,string search)
        {
            if (search==null)
            {
                search = "";
            }

            int count;
            List<Course> list = CourseService.GetCoursesInfo(page,limit,search,out count);

            var data = new
            {
                code = 0,
                count,
                msg = "success",
                data = list.Select(c => new
                {
                    id = c.ID,
                    code = c.CourseCode,
                    name = c.Name
                })
            };

            return data;

        }

        [HttpGet]
        [Route("api/deletecourse")]
        public void DeleteCourse(int id)
        {
            CourseService.Delete(id);
        }

        /// <summary>
        /// 获取所有老师，以及当前课程任课老师、用于穿梭框
        /// </summary>
        /// <param name="courseID"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/getcourseteacher")]
        public object GetCourseTeacher(int courseID)
        {
            var allTeachers = TeacherService.GetAllTeachers();

            var courserTeachers = CourseService.CourserTeachers(courseID);

            var allTeacherData = allTeachers.Select(r => new
            {
                name = r.Name,
                id = r.ID
            });

            var data = courserTeachers.Select(d => d.ID).ToList();


            return new
            {
                allTeacher = allTeacherData,
                courseTeacherIDs= data
            };
        }

        [HttpPost]
        [Route("api/givecourseaddteacher")]
        public void GiveCourseAddTeacher([FromBody]JObject data)
        {
//          var teacherList = data[""].Values<int>().ToList();
            var teacherList = data.Value<JArray>("tl").ToObject<List<int>>();
            var courseID = Convert.ToInt32(data["courseID"]);

            CourseService.GiveOrRemoveCourseTeacher(teacherList, courseID,1);
        }

        [HttpPost]
        [Route("api/removecourseaddteacher")]
        public void RemoveCourseAddTeacher([FromBody] JObject data)
        {
            var teacherList = data[""].Values<int>().ToList();
            var courseID = Convert.ToInt32(data["courseID"]);

            CourseService.GiveOrRemoveCourseTeacher(teacherList, courseID,0);
        }

        /// <summary>
        /// 获取班级已有课程
        /// </summary>
        /// <param name="cid">班级ID</param>
        /// <param name="syId">学年ID </param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/getclass_course")]
        public object GetClassCourse(int cid,int syId, int page, int limit)
        {
            int count;
            var list = CourseService.GetClassCourse(cid, syId,out count,page, limit);

            var data = new
            {
                code = 0,
                msg = "",
                count = 0,
                data = list.Select(c => new
                {
                    id = c.ID,
                    courseName = c.Name,
                    teacherName = c.Teacher.Name
                })
            };
            return data;
        }

        /// <summary>
        /// 获取班级没有的课程
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("api/course/classhavenocourse")]
        public object GetClassHaveNoCourse(int classId,int studyYear)
        {
            List<Course> list = CourseService.GetClassHaveNoCourse(classId, studyYear);
            var data = list.Select(s => new
            {
                id = s.ID,
                name = s.Name
            });

            return data;
        }
        
        /// <summary>
        /// 添加课程
        /// </summary>
        /// <param name="data"></param>
        [HttpPost]
        [Route("api/insertcourse")]
        public void InsertCourse([FromBody] JObject data) {
            //解析数据
            string name = data["name"].ToString();
            string courseCode = data["courseCode"].ToString();
            bool isAlive = true;
            CourseService.InsertCourse(name, courseCode, isAlive);

        }

        /// <summary>
        /// 获取可任课老师
        /// 不用于穿梭框
        /// </summary>
        /// <param name="courseId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/course/getcourseteachers")]
        public object GetCourseTeachers(int courseId)
        {
            var teacher = CourseService.CourserTeachers(courseId);
            var data = teacher.Where(r=>r.IsFreeze==false).Select(r => new
            {
                id = r.ID,
                name = r.Name
            });
            return data;
        }

        /// <summary>
        /// 为班级添加课程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/course/classaddcourse")]
        public object GiveClassAddCourse([FromBody]JObject data)
        {
            int classId = data.Value<int>("classId");
            int studyYear = data.Value<int>("studyYear");
            int courseId = data.Value<int>("courseId");
            int teacherId = data.Value<int>("teacherId");
            return CourseService.ClassAddCourse(classId, studyYear, courseId, teacherId);
        }
    }
}
